package com.google.code.sms.controller;

import com.google.code.sms.domain.MediaFolder;
import com.google.code.sms.domain.User;
import com.google.code.sms.service.SecurityService;
import com.google.code.sms.service.SettingsService;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.ParameterizableViewController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller for the top frame.
 *
 * @author Sindre Mehus
 */
public class TopController extends ParameterizableViewController {

    private SettingsService settingsService;
    private SecurityService securityService;

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<>();

        List<MediaFolder> allMediaFolders = settingsService.getAllMediaFolders();
        User user = securityService.getCurrentUser(request);

        map.put("user", user);
        map.put("mediaFoldersExist", !allMediaFolders.isEmpty());

        ModelAndView result = super.handleRequestInternal(request, response);
        result.addObject("model", map);
        return result;
    }

    public void setSettingsService(SettingsService settingsService) {
        this.settingsService = settingsService;
    }

    public void setSecurityService(SecurityService securityService) {
        this.securityService = securityService;
    }
}
